package com.wanmait.happyheating.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.City;
import com.wanmait.happyheating.pojo.HeatingNumber;
import com.wanmait.happyheating.pojo.Province;
import com.wanmait.happyheating.service.CityService;
import com.wanmait.happyheating.service.HeatingNumberService;
import com.wanmait.happyheating.service.ProvinceService;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 前后端分离注解
 */
@RestController
/**
 * 前后端分离的注解
 */
@CrossOrigin
@RequestMapping("/manage")
public class AdminHeatingNumberController {
    @Resource
    private HeatingNumberService heatingNumberService;
    @Resource
    private ProvinceService provinceService;
    @Resource
    private CityService cityService;

    /**
     * 陈宗    2021-05-31
     * Controller的供暖号码表查询并且分页的方法
     *
     * @param
     * @param pageNum
     * @return
     */
    @PostMapping("heatingNumber")
    public Result heatingNumber(@RequestBody HeatingNumber heatingNumber, Integer pageNum) {
        //供暖号码id为0时,设置供暖号码id为null
        if (heatingNumber.getId() == 0) {
            heatingNumber.setId(null);
        }
        //第几页为null时,显示第一页
        if (pageNum == null) {
            pageNum = 1;
        }
        //调用业务逻辑层的供暖号码查询所有的方法
        PageInfo<HeatingNumber> pageInfo = heatingNumberService.findPage(pageNum, heatingNumber);
        return Result.success(pageInfo);
    }

    /**
     * 陈宗  2021-06-02
     * Controller查询所有的省份的方法
     */
    @GetMapping("heatingNumber")
    public Result heatingNumber() {
        //调用业务逻辑层查询所有的省份的方法
        List<Province> provinces = provinceService.list();
        return Result.success(provinces);
    }

    /**
     * 陈宗  2021-06-03
     * Controller根据省份id查询所有相对应的城市
     */
    @GetMapping("heatingNumberCity")
    public Result heatingNumberCity( Integer id) {
        //调用业务逻辑层查询所有的省份的方法
        List<City> citys = cityService.findProvinceId(id);
        return Result.success(citys);
    }


    /**
     * 陈宗 2021-06-01
     * Controller添加供暖号码表信息的方法
     */
    @PostMapping("addHeatingNumber")
    public Result addHeatingNumber(@RequestBody HeatingNumber heatingNumber) {
        heatingNumberService.save(heatingNumber);
        return Result.success("添加成功");
    }

    /**
     * 陈宗  2021-06-02
     * Controller根据供暖号码的id对供暖号码表的信息进行修改的方法
     */
    @PostMapping("updateHeatingNumber")
    public Result updateHeatingNumber(@RequestBody HeatingNumber heatingNumber) {
        heatingNumberService.updateById(heatingNumber);
        return Result.success("修改成功");
    }

    /**
     * @PathVariable是spring一个新功能：接收请求路径中占位符的值 陈宗   2021-06-02
     * Controller 根据供暖号码id对供暖号码的信息进行删除的方法
     */
    @PostMapping("/deleteHeatingNumber/{id:\\d+}")
    public Result deleteHeatingNumber(@PathVariable Integer id) {
        heatingNumberService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     *批量删除客户端提交的数组，服务器端用Integer[]ids或者List<Integer>ids都可以
     * 陈宗   2021-06-04
     * Controller 根据供暖号码id对供暖号码的信息进行删除的方法
     */
    @PostMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        heatingNumberService.removeByIds(ids);
        return Result.success("批量删除成功");
    }

}
